package com.bigfreakindeals.DAO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


/**
 * Base data access class
 * @author phong
 *
 */
public abstract class DAO {
	/**
	 * Database helper
	 */
	protected DatabaseHelper dbHelper = null;
	/**
	 * Table name
	 */
	protected String tableName;
	/**
	 * Application Context
	 */
	protected Context appCtx;
	private boolean canUpgrade = true;
	/**
	 * Abstract function called to create the table
	 */
	protected abstract void createTable();
	
	public DAO(Context appCtx, String tablename, DatabaseHelper helper ) {
		this.appCtx = appCtx;
		this.tableName = tablename;
		this.dbHelper = helper;
		this.initialiseDatabase();
	}
	
	public DAO(Context appCtx, String tablename, DatabaseHelper helper, boolean canUpgrade) {
		this.appCtx = appCtx;
		this.tableName = tablename;
		this.dbHelper = helper;
		this.canUpgrade = canUpgrade;
		this.initialiseDatabase();
	}
	/**
	 * Close the connection
	 */
	/*
	private void close() {
		this.dbHelper.close();
	}
	*/
	/**
	 * Initialise the database
	 * @param createString
	 */
	protected void initialiseDatabase() {
		if( this.dbHelper == null )
			this.dbHelper = new DatabaseHelper(appCtx);
		
		//Log.d("DatabaseHelper", tableName+":requireUpgrade:"+this.dbHelper.requireUpgrade());
		if( this.dbHelper.requireUpgrade() && canUpgrade ) {
			//Log.d("DAO", "Upgrading: " + this.tableName);
			String query = "DROP TABLE " + this.tableName + ";";
			SQLiteDatabase db = null;
			try {
				db = this.getDatabase();
				db.execSQL(query);
			} catch( Exception ex ) {
			}
		}
			
		this.createTable();
	}
	/**
	 * Get the database instance
	 * @return
	 */
	protected SQLiteDatabase getDatabase() {
		return this.dbHelper.getDatabase();
	}
	/**
	 * Check if the cursor has rows
	 * @param cursor
	 * @return
	 */
	protected boolean cursorHasRows(Cursor cursor) {
		return (cursor!=null && cursor.getCount()>0);
	}
	
	public void clearTable() {
		SQLiteDatabase db = null;
		try {
			db = this.getDatabase();
			db.delete(this.tableName, null, null);
		} catch( Exception ex ) {
		}		
	}	
	

	public boolean insertTable(ContentValues values, String whereClause, String[] whereArgs){

		SQLiteDatabase db = null;
		try {
			db = this.getDatabase();
			//Log.d("DAO", "insertTable:"+tableName);
			if ( db.update(tableName, values, whereClause, whereArgs)<=0 )
				db.insertOrThrow(tableName, null, values);
			return true;
		} catch( Exception ex ) {
			//Log.e("DAO", "insertTable:exception:"+ex);
		}	
		return false;
		//DBInsertHandler.getInstance(dbHelper).addQueue(tableName, values, whereClause, whereArgs);
	}
}
